<template>
    <div class="flex-1 flex flex-col min-h-0">
        <el-table
            v-loading="pager.loading"
            :data="pager.lists"
            size="large"
            class="mt-4"
            style="height: 100%"
        >
            <el-table-column label="订单编号" prop="order_sn" width="200" />
            <el-table-column label="套餐名称" prop="package_name" min-width="100" />
            <el-table-column label="支付方式" prop="pay_way" min-width="100" />
            <el-table-column label="实付金额" prop="paid_amount" min-width="100">
                <template #default="scope: { row: RechargeBillingResponse }">
                    ￥{{ scope.row.paid_amount }}
                </template>
            </el-table-column>
            <el-table-column label="支付状态" prop="pay_status" min-width="100">
                <template #default="scope: { row: RechargeBillingResponse }">
                    <el-tag type="success">{{ scope.row.pay_status }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="支付时间" prop="pay_time" min-width="175" />
            <el-table-column label="操作" width="110" fixed="right">
                <template #default="scope: { row: RechargeBillingResponse }">
                    <el-button
                        type="primary"
                        link
                        @click="handleDetail(scope.row)"
                    >
                        订单详情
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div v-if="pager.lists.length > 0" class="flex justify-end mt-4">
            <paging
                v-model="pager"
                layout="prev, pager, next"
                @change="queryLists"
            />
        </div>

        <ClientOnly>
            <popup
                :show="showPop"
                width="600px"
                title="订单详情"
                confirmButtonText=""
                cancelButtonText=""
                @close="showPop = false"
            >
                <div class="p-6 pt-2">
                    <el-form label-width="90px" label-position="left">
                        <el-form-item label="订单编号：" style="margin-bottom: 5px;">
                            {{ details.order_sn }}
                        </el-form-item>
                        <el-form-item label="套餐名称：" style="margin-bottom: 5px;">
                            {{ details.package_name }}
                        </el-form-item>
                        <el-form-item label="支付方式：" style="margin-bottom: 5px;">
                            {{ details.pay_way }}
                        </el-form-item>
                        <el-form-item label="实付金额：" style="margin-bottom: 5px;">
                            {{ details.paid_amount }}
                        </el-form-item>
                        <el-form-item label="支付时间：" style="margin-bottom: 5px;">
                            {{ details.create_time }}
                        </el-form-item>

                        <el-divider content-position="left">套餐内容</el-divider>

                        <el-form-item v-if="details.points_num" label="积分数量：" style="margin-bottom: 5px;">
                            <span class="text-success font-bold">+{{ details.points_num }}</span>
                        </el-form-item>
                        <el-form-item v-if="details.train_num" label="训练组数：" style="margin-bottom: 5px;">
                            <span class="text-success font-bold">+{{ details.train_num }}</span>
                        </el-form-item>
                        <el-form-item v-if="details.robot_num" label="智能体数：" style="margin-bottom: 5px;">
                            <span class="text-success font-bold">+{{ details.robot_num }}</span>
                        </el-form-item>
                        <el-form-item v-if="details.know_num" label="知识库数：" style="margin-bottom: 5px;">
                            <span class="text-success font-bold">+{{ details.know_num }}</span>
                        </el-form-item>
                    </el-form>
                </div>
            </popup>
        </ClientOnly>
    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/composables/usePaging'
import rechargeApi from '~/api/recharge/index'

// 订单详情数据
const showPop = ref<boolean>(false)
const details = ref({} as RechargeBillingResponse)

/**
 * 分页获取订单
 */
const { pager, queryLists } = usePaging({
    fetchFun: rechargeApi.billing
})

/**
 * 处理订单详情
 */
const handleDetail = (row: RechargeBillingResponse) => {
    details.value = row
    showPop.value = true
}

onMounted(async () => {
    await queryLists()
})
</script>
